<?php

namespace app\api\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 会员提现
 *
 * api-MemberWithdrawal
 *
 * @Apidoc\Title("会员提现")
 * @Apidoc\Group("api")
 */
class MemberWithdrawal extends Auth
{
    /**
     * list
     * @Apidoc\Title("列表")
     */
    public  function list()
    {
        $list_rows = input('list_rows', 10, 'intval');
        $data = \app\common\model\MemberWithdrawal::where('member_id', $this->request->uid)
            ->append(['remark'])
            ->withAttr('remark', function($value, $data) {
                $arr = explode('__',$data['name']);
                return '支付宝账号:' . $arr[0] . "\n" . "姓名:" . $arr[1]. "\n" . "关联收益id:" . $arr[2];
            })
            ->order('id desc')->paginate($list_rows);
        return success('', $data);
    }

    /**
     * add
     * @Apidoc\Title("新增")
     */
    public function add()
    {
        $remark = input('remark');
        $money = \app\common\model\MemberBenefits::where('member_id', $this->request->uid)
            ->where('state', 0)->sum('money');
        $ids = \app\common\model\MemberBenefits::where('member_id', $this->request->uid)
            ->where('state', 0)->column('id');
        \app\common\model\MemberBenefits::where('member_id', $this->request->uid)
            ->where('state', 0)->save(['state' => 1]);
        $ids = implode(',', $ids);
        \app\common\model\MemberWithdrawal::create([
            'name' =>  $remark . '__' . $ids,
            'money' => $money,
            'member_id' => $this->request->uid
        ]);
        return success();

    }
}
